<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>架子鼓demo</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        .keys {
            width: 1080px;
            height: 150px;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            display: flex;
            justify-content: space-around;
        }

        .key {
            width: 100px;
            height: 80px;
            margin:1rem;
            transition:all .07s;
            background-color: rgb(153, 153, 153);
            border: 2px solid black;
            margin-top: 35px;
            display: flex;
            flex-direction: column;
            font-size: 24px;
            line-height: 40px;
            text-align: center;
            color: #fff;
        }

        .key>.sound {
            font-size: 14px;
            color: #ffc600;
        }

        .chufa {
            border: 2px solid #ffc600;
            transform: scale(1.1);
        }
    </style>
</head>

<body>

    <div class="keys">
        <div data-key="65" class="key">
            <kbd>A</kbd>
            <span class="sound">clap</span>
        </div>
        <div data-key="83" class="key">
            <kbd>S</kbd>
            <span class="sound">hihat</span>
        </div>
        <div data-key="68" class="key">
            <kbd>D</kbd>
            <span class="sound">kick</span>
        </div>
        <div data-key="70" class="key">
            <kbd>F</kbd>
            <span class="sound">openhat</span>
        </div>
        <div data-key="71" class="key">
            <kbd>G</kbd>
            <span class="sound">boom</span>
        </div>
        <div data-key="72" class="key">
            <kbd>H</kbd>
            <span class="sound">ride</span>
        </div>
        <div data-key="74" class="key">
            <kbd>J</kbd>
            <span class="sound">snare</span>
        </div>
        <div data-key="75" class="key">
            <kbd>K</kbd>
            <span class="sound">tom</span>
        </div>
        <div data-key="76" class="key">
            <kbd>L</kbd>
            <span class="sound">tink</span>
        </div>
    </div>
  <audio data-key="65" src="sounds/clap.wav"></audio>
  <audio data-key="83" src="sounds/hihat.wav"></audio>
  <audio data-key="68" src="sounds/kick.wav"></audio>
  <audio data-key="70" src="sounds/openhat.wav"></audio>
  <audio data-key="71" src="sounds/boom.wav"></audio>
  <audio data-key="72" src="sounds/ride.wav"></audio>
  <audio data-key="74" src="sounds/snare.wav"></audio>
  <audio data-key="75" src="sounds/tom.wav"></audio>
  <audio data-key="76" src="sounds/tink.wav"></audio>

</body>

<script>

    // 判断按键事件是否结束，移除 .chufa 
    function removeChufa(event){
        // 如果没有这个的时候，可以直接结束函数
        if(event.propertyName !== 'transform') return;
        // 如果有这个的话，就要移除掉给其添加的触发class属性
        event.target.classList.remove("chufa")
    }
    // 按键触发事件
    function playSound(event){

        // 获取应该播放那个audio
        const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`)
        // 获取是那个按键被触发了
        const key = document.querySelector(`div[data-key="${event.keyCode}"]`)
        if (!audio) return; // 处理无效的按键事件
        // 按键触发时添加class属性
        key.classList.add("chufa")
        audio.currentTime = 0; // 每次播放之后都使音频播放进度归零
        audio.play()  //播放对应音效
    }

    // 按键事件结束后 调用函数清理class属性
    const keys = Array.from(document.querySelectorAll(".key"))
    keys.forEach(key => key.addEventListener('transitionend',removeChufa))

    // 监听按键
    window.addEventListener('keydown', playSound);


</script>

</html>